<template>
  <view class="network-demo">
    <view class="header">
      <text class="title">网络请求模块演示</text>
    </view>
    
    <view class="section">
      <text class="section-title">代理配置测试</text>
      <view class="button-group">
        <button @click="testApiProxy" class="test-btn">测试 /api 代理</button>
        <button @click="testDevApiProxy" class="test-btn">测试 /dev-api 代理</button>
      </view>
    </view>
    
    <view class="section">
      <text class="section-title">API模块测试</text>
      <view class="button-group">
        <button @click="testUserApi" class="test-btn">测试用户API</button>
        <button @click="testMarketApi" class="test-btn">测试行情API</button>
        <button @click="testPositionApi" class="test-btn">测试持仓API</button>
      </view>
    </view>
    
    <view class="section">
      <text class="section-title">请求结果</text>
      <view class="result-area">
        <text class="result-text">{{ resultText }}</text>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/utils/request.js'
import { userApi, marketApi, positionApi } from '@/utils/api.js'

export default {
  data() {
    return {
      resultText: '点击按钮测试网络请求功能...'
    }
  },
  
  methods: {
    // 测试 /api 代理
    async testApiProxy() {
      this.resultText = '正在测试 /api 代理...'
      try {
        const response = await request.get('/api/health')
        this.resultText = `✅ /api 代理测试成功:\n${JSON.stringify(response, null, 2)}`
      } catch (error) {
        this.resultText = `❌ /api 代理测试 (预期错误，无后端服务):\n状态码: ${error.statusCode}\n错误信息: ${error.message}`
      }
    },
    
    // 测试 /dev-api 代理
    async testDevApiProxy() {
      this.resultText = '正在测试 /dev-api 代理...'
      try {
        const response = await request.get('/dev-api/test')
        this.resultText = `✅ /dev-api 代理测试成功:\n${JSON.stringify(response, null, 2)}`
      } catch (error) {
        this.resultText = `❌ /dev-api 代理测试 (预期错误，无后端服务):\n状态码: ${error.statusCode}\n错误信息: ${error.message}`
      }
    },
    
    // 测试用户API
    async testUserApi() {
      this.resultText = '正在测试用户API...'
      try {
        const response = await userApi.getUserInfo()
        this.resultText = `✅ 用户API测试成功:\n${JSON.stringify(response, null, 2)}`
      } catch (error) {
        this.resultText = `❌ 用户API测试 (预期错误，无后端服务):\n状态码: ${error.statusCode}\n错误信息: ${error.message}`
      }
    },
    
    // 测试行情API
    async testMarketApi() {
      this.resultText = '正在测试行情API...'
      try {
        const response = await marketApi.getQuotes({ symbol: 'AAPL' })
        this.resultText = `✅ 行情API测试成功:\n${JSON.stringify(response, null, 2)}`
      } catch (error) {
        this.resultText = `❌ 行情API测试 (预期错误，无后端服务):\n状态码: ${error.statusCode}\n错误信息: ${error.message}`
      }
    },
    
    // 测试持仓API
    async testPositionApi() {
      this.resultText = '正在测试持仓API...'
      try {
        const response = await positionApi.getList({ page: 1, size: 10 })
        this.resultText = `✅ 持仓API测试成功:\n${JSON.stringify(response, null, 2)}`
      } catch (error) {
        this.resultText = `❌ 持仓API测试 (预期错误，无后端服务):\n状态码: ${error.statusCode}\n错误信息: ${error.message}`
      }
    }
  }
}
</script>

<style scoped>
.network-demo {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.section {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
  display: block;
}

.button-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.test-btn {
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 12px 20px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.test-btn:hover {
  background-color: #0056b3;
}

.test-btn:active {
  background-color: #004494;
}

.result-area {
  background-color: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 6px;
  padding: 15px;
  min-height: 100px;
  max-height: 300px;
  overflow-y: auto;
}

.result-text {
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.5;
  color: #333;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>